var Project = {
    tableId: "projectTable"
};
var numberPage = 1;
layui.use(['table', 'admin', 'ax', 'form', 'layer', 'jquery'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;

    /**
     * 检索区域
     */
    var qyAjax = new $ax("/project/getAreaByType");
    qyAjax.set("type", 2);
    var qyResult = qyAjax.start();
    $.each(qyResult, function (index, data) {
        $("#provinceId").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
    });
    form.render();
    //YYJL
    var ajax1 = new $ax(Feng.ctxPath + "/project/selectYyjlList", function (data) {
        var sb;
        for(let i = 0; i < data.length; i++) {
            sb+='<option value="'+data[i].tel+'">' + data[i].yyjl + '</option>';
        }
        $("#yyjl").append(sb);
        form.render();
    }, function (data) {
        Feng.error(data.responseJSON.message);
    });
    ajax1.start();

    //DD
    form.on('select(yyjlfilte)', function(data){
        $('#dd option').not('option:first').remove();
        if(data.value!=''){
            var ajax1 = new $ax(Feng.ctxPath + "/project/selectDdList", function (data) {
                var sb;
                for(let i = 0; i < data.length; i++) {
                    sb+='<option value="'+data[i].tel+'">' + data[i].dd + '</option>';
                }
                $("#dd").append(sb);
                form.render();
            }, function (data) {
                Feng.error(data.responseJSON.message);
            });
            ajax1.set("phone",data.value);
            ajax1.start();
        }
    });


    var dd = new Date();
    var curtyear = dd.getFullYear();
    Project.getDate = function(){
        var m = dd.getMonth()+1;//获取当前月份的日期
        var d = dd.getDate();
        m=m < 10?("0" + m):m;
        d=d < 10?("0" + d):d;
        return curtyear+"-"+m+"-"+d;
    }
    var dateHtml='';
    for(var i=2015;i<=curtyear;i++){
        dateHtml+='<option value="'+i+'">'+i+'</option>';
    }
    $('#year').append(dateHtml);
    form.render();

    /**
     * 初始化表格的列
     */
    Project.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers', title: '序号', align: "center", fixed: true, width: 46},
            {field: 'name', sort: true, title: '工程名称', align: "left", fixed: true, width: 260},
            {field: 'kfName', title: '开发人员', align: "left",width:100},
            {field: 'yyjlName', title: '营运经理', align: "left",width:100},
            {field: 'ddName', sort: false, title: '督导', align: "left",width:100},
            {field: 'ctriqi', sort: true, title: '出图日期', align: "left"},
            {field: 'kgriqi', sort: true, title: '开工时间', align: "left"},
            {field: 'jdriqi', sort: true, title: '预计完工时间', align: "left"},
            {field: 'wgriqi', sort: true, title: '实际完工时间', align: "left"},
            {field: 'yjkyriqi', sort: true, title: '预计开业时间', align: "left"},
            {field: 'kyriqi', sort: true, title: '实际开业时间', align: "left",templet:function(d){
                if(d.kyriqi!=null && d.kyriqi!='' && d.kyriqi!=undefined){
                    return d.kyriqi.substring(0,10);
                }else {
                    return d.kyriqi;
                }
                }},
            {field: 'status', sort: true, title: '状态', align: "left", templet:function(d){
                switch (d.status) {
                    case '1': return '新建中';break;
                    case '2': return '审批中';break;
                    case '3': return '对接中';break;
                    case '4': return '装修中';break;
                    case '5': return '筹备中';break;
                    case '6': return '已开业';break;
                }
                }}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Project.search = function () {
        var queryData = {};
        queryData['year'] = $("#year").val();
        queryData['provinceId'] = $("#provinceId").val();
        queryData['status'] = $("#status").val();
        queryData['yyjl'] = $("#yyjl").val();
        queryData['dd'] = $("#dd").val();

        Project.tableCfg.url=Feng.ctxPath + '/project/jindu';

        var todayval = $('#kytoday').prop("checked");
        if(todayval){
            queryData={kyDate:Project.getDate()};
        }
        Project.tableCfg.where=queryData;
        table.render(Project.tableCfg);
        sessionStorage.setItem("jindu",JSON.stringify(queryData));
    };

    form.on('checkbox(kytoday)', function(data){
        var t=data.elem.checked;
        if(t){
            Project.tableCfg.where={kyDate:Project.getDate};
            $("#year").val('');
            $("#provinceId").val('');
            $("#status").val('');
            $("#yyjl").val('');
            $("#dd").val('');
        }else{
            var  para = sessionStorage.getItem("jindu");
            if(para!=null){
                var p=JSON.parse(para);
                Project.tableCfg.where=p;
                $('#year').val(p.year);
                $('#provinceId').val(p.provinceId);
                $('#status').val(p.status);
                $('#yyjl').val(p.yyjl);
                $('#dd').val(p.dd);
            }
        }
        form.render();
        Project.tableCfg.url=Feng.ctxPath + '/project/jindu';
        table.render(Project.tableCfg);
    });

    /**
     * 导出excel按钮
     */
    Project.exportExcel = function () {
        var todayval = $('#kytoday').prop("checked");
        var p="";
        if(todayval){
            p='kyDate='+Project.getDate();
        }else{
            var  para = sessionStorage.getItem("jindu");
            if(para!=null){
                var o=JSON.parse(para);
                p='provinceId='+o.provinceId+'&year='+o.year+'&yyjl='+o.yyjl+'&dd='+o.dd+'&status='+o.status;
            }
        }
        window.location.href=Feng.ctxPath + '/project/jinduExport?'+p;
    };

    /**
     * 渲染表格
     */
    Project.tableCfg={
        elem: '#' + Project.tableId,
        page: true,
        data:[],
        cellMinWidth: 100,
        even: true,
        limit:20,
        height: "full-59",
        cols: Project.initColumn(),
        done: function (res, page, count) {
            if ($("#pageA").val() != null && $("#pageA").val() != "" && numberPage == 1) {
                page = $("#pageA").val();
                numberPage = 0;
                table.reload(Project.tableId, {page: {curr: page}}, function () {
                    $("#pageA").val("");
                });
            }
            var number = $("#changeDM").children().length;
            let tableView = this.elem.next();
            layui.each(res.data, function (i, item) {
                if (number == 0) {
                    tableView.find('tr[data-index=' + i + ']').find('td').data('edit', false);
                }
            })
        }
    };
    var  para = sessionStorage.getItem("jindu");
    if(para!=null){
        Project.tableCfg.url=Feng.ctxPath + '/project/jindu';
        var p=JSON.parse(para);
        Project.tableCfg.where=p;
        if(p.provinceId!=''){
            $('#provinceId').val(p.provinceId);
        }
        if(p.year!=''){
            $('#year').val(p.year);
        }
        if(p.name!=''){
            $('#address').val(p.name);
        }
        if(p.yyjl!=''){
            $('#yyjl').val(p.yyjl);
        }
        if(p.dd!=''){
            $('#dd').val(p.dd);
        }
        if(p.status!=''){
            $('#status').val(p.status);
        }
        form.render('select');
    }
    table.render(Project.tableCfg);
    /**
     * 搜索按钮点击事件
     */
    $('#btnSearch').click(function () {
        Project.search();
    });
    Project.search();

    /**
     * 导出excel
     */
    $('#export').click(function () {
        Project.exportExcel();
    });

    /**
     * 工具条点击事件
     */
    /*table.on('tool(' + Project.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        $("#ida").val(data.id);
        if (layEvent === 'manager') {
            Project.zhiDing("营运经理", "0", "40");
        }
    });*/

    /**
     * 是否闭店
     */
    form.on('select(cashierAccountYn)', function (data) {
        var cashierAccountYn = data.value;
        if (cashierAccountYn == 1) {
            $('#open').hide();
            $('#close').show();
        } else if (cashierAccountYn == 0) {
            $('#close').hide();
            $('#open').show();
        }
    });

    Project.reloadtable = function (page) {
        table.reload(Project.tableId, {
            where: queryData, page: {curr: page}
        });
    };

});
